package com.tjetc.service;

import com.tjetc.dao.CoachDao;
import com.tjetc.domain.Coach;
import com.tjetc.domain.Person;
import com.tjetc.utils.Db;
import org.apache.commons.dbutils.QueryRunner;
import com.tjetc.domain.*;

import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;

public class CoachService {
    private CoachDao coachDao=new CoachDao();
    public Person log(String username, String password) {
        try {
            return coachDao.log(username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Coach> showCoachSalary() {
        try {
            return coachDao.showCoachSalary();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Coach> showCoach() {
        try {
            return coachDao.showCoach();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Coach findByCoachName(String coachName) {
        try {
            return coachDao.findByCoachName(coachName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean update(int coachId, String coachUserName, String coachPassWord, String coachName1, int coachAge, int coachExperience, int coachTeamId, int coachSalary, String coachName) {
        QueryRunner queryRunner = new QueryRunner();
        //开启事务
        try {
            Db.start();
            //修改教练信息
            coachDao.update(coachId,coachUserName,coachPassWord,coachName1,coachAge,coachExperience,coachTeamId,coachSalary,coachName);
            Db.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }

        return false;
    }

    public boolean dismiss(String coachName) {
        QueryRunner queryRunner = new QueryRunner();
        //开启事务
        try {
            Db.start();
            //修改教练信息
            coachDao.dismiss(coachName);
            Db.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }

        return false;
    }



    public Coach findByname(String name) {
        try {
            return coachDao.findByname(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Team findTeamByTeamId(int teamId) {
        try {
            return coachDao.findTeamByTeamId(teamId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean delCoachByname(String name) {
        try {
            return coachDao.delCoachByname(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateUsername(String newUsername, String name) {
        try {
            return coachDao.updateUsername(newUsername,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updatePassword(String newPassword, String name) {
        try {
            return coachDao.updatePassword(newPassword,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateTeamId(String newTeamId, String name) {
        try {
            return coachDao.updateTeamId(newTeamId,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateSalary(String newSalary, String name) {
        try {
            return coachDao.updateSalary(newSalary,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Coach findByUsername(String username) {
        try {
            return coachDao.findByUsername(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean addPlayers(List<Player> players) {
        try {
            QueryRunner runner=new QueryRunner();
            Db.start();
            for (Player player : players) {
                coachDao.addPlayer(player);
            }
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        }finally {
            Db.close();
        }
        return false;
    }

    public Player findPlayerByName(String name) {
        try {
            return coachDao.findPlayerByName(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean delPlayerByName(String name) {
        try {
            return coachDao.delPlayerByName(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void showPlayer(int teamId) {
        List<Player> players;
        try {
            players=coachDao.getPlayersByTeamId(teamId);
            if (players!=null) {
                for (Player player : players) {
                    System.out.println("球员姓名：" + player.getName());
                    System.out.println("球员年龄：" + player.getAge());
                    System.out.println("球员身高：" + player.getHeight());
                    System.out.println("球员体重：" + player.getWeight());
                    System.out.println("球员位置：" + player.getPosition());
                }
            }else {
                System.out.println("本队暂无球员");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void showAllPlayer() {
        List<Player> players;
        try {
            players= coachDao.getAllPlayer();
            if (players!=null) {
                for (Player player : players) {
                    System.out.println("球员姓名：" + player.getName());
                    System.out.println("球员年龄：" + player.getAge());
                    System.out.println("球员身高：" + player.getHeight());
                    System.out.println("球员体重：" + player.getWeight());
                    System.out.println("球员位置：" + player.getPosition());
                }
            }else {
                System.out.println("暂无球员,请联系管理员添加");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public List<Game> findGameByTeamId(String homeTeamName) {
        try {
            return coachDao.findGameByHomeTeamId(homeTeamName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Game findGameById(int id) {
        try {
            return coachDao.findGameById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Game> findGameByTime(Date date) {
        try {
            return coachDao.findGameByTime(date);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean updataGameTime(Date date, int id) {
        try {
            return coachDao.updataGameTime(date,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delGame(int id) {
        try {
            return coachDao.delGame(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void showMyGame(int teamId) {
        List<Game> games;
        try {
            games=coachDao.getMyGame(teamId);
            if (games!=null) {
                for (Game game : games) {
                    System.out.println("地点:" + game.getLocation());
                    System.out.println("时间:" + game.getTime());
                    System.out.println("主队" + game.getHomeTeamId());
                    System.out.println("客队:" + game.getAwayTeamId());
                }
            }else {
                System.out.println("暂时没有球队比赛!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void showAllGame() {
        List<Game> games;
        try {
            games=coachDao.getAllGame();
            if (games!=null) {
                for (Game game : games) {
                    System.out.println("地点:" + game.getLocation());
                    System.out.println("时间:" + game.getTime());
                    System.out.println("主队" + game.getHomeTeamId());
                    System.out.println("客队:" + game.getAwayTeamId());
                }
            }else {
                System.out.println("暂时没有球队比赛!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void Teamanalysis() {
        List<TeamScore> teamScores;
        try {
            teamScores= coachDao.Teamanalysis();
            if (teamScores!=null){
                for (TeamScore teamScore : teamScores) {
                    if (coachDao.findTeamByTeamId(teamScore.getTeamId())!=null) {
                        System.out.println("球队名:" + coachDao.findTeamByTeamId(teamScore.getTeamId()).getName());
                        System.out.println("得分:" + teamScore.getScore());
                    }
                }
            }else {
                System.out.println("暂无得分信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Playeranalysis() {
        List<PlayerScore> playerScores;
        try {
            playerScores= coachDao.Playeranalysis();
            if (playerScores!=null){
                for (PlayerScore ignored : playerScores) {
                    if (coachDao.findTeamByTeamId(coachDao.findGameById(ignored.getGameId()).getHomeTeamId())!=null && coachDao.findTeamByTeamId(coachDao.findGameById(ignored.getGameId()).getAwayTeamId())!=null ) {
                        Player player = coachDao.findPlayerById(ignored.getPlayerId());
                        System.out.println("主队:" + coachDao.findTeamByTeamId(coachDao.findGameById(ignored.getGameId()).getHomeTeamId()).getName());
                        System.out.println("客队:" + coachDao.findTeamByTeamId(coachDao.findGameById(ignored.getGameId()).getAwayTeamId()).getName());
                        System.out.println("姓名:" + player.getName());
                        System.out.println("得分:" + ignored.getScore());
                    }
                }
            }else {
                System.out.println("暂无得分信息");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Detail findDetailById(int id) {
        try {
            return coachDao.findDetailById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean addTraining(int teamId, int id) {
        try {
            return coachDao.addTraining(teamId,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Detail getDetailBytime(int id, int time) {
        try {
            return coachDao.getDetailBytime(id, time);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean showTraining(int teamId) {
        List<Detail> details = new ArrayList<>();
        try {
            details = coachDao.showTraining(teamId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (details!=null){
            for (Detail detail : details) {
                System.out.println("项目序号:" + detail.getId());
                System.out.println("项目:" + detail.getItem());
                System.out.println("时间:" + detail.getTime());
            }
            return true;
        }else {
            System.out.println("您执教的球队暂时没有项目，请添加");
            return false;
        }

    }

    public boolean updataTraining(int trainingId, int detailId) {
        try {
            return coachDao.updataTraining(trainingId,detailId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delTraining(int id) {
        try {
            return coachDao.delTraining(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean addDetail(String s, int time) {
        try {
            return coachDao.addDetail(s,time);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Detail findDetail(String s, int time) {
        try {
            return coachDao.findDetail(s,time);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean updateDeatail(String s, int time, int id) {
        try {
            return coachDao.updateDeatail(s,time,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<Detail> findDetailByname(String name) {
        try {
            return coachDao.findDetailByname(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean delDetail(String name, int time) {
        try {
            return coachDao.delDetail(name,time);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void showDetail() {
        List<Detail> details = new ArrayList<>();
        try {
            details=coachDao.showDetail();
            if (details!=null) {
                for (Detail detail : details) {
                    System.out.println("项目序号:" + detail.getId());
                    System.out.println("项目名称:" + detail.getItem());
                    System.out.println("项目时间:" + detail.getTime()+"点");
                }
            }else {
                System.out.println("暂时没有项目，请添加");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Equip findEquipByName(String name) {
        try {
            return coachDao.findEquipByName(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean addEquip(String name, double price, int num) {
        try {
            return coachDao.addEquip(name,price,num);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updataequip(String name, double price, int num) {
        try {
            return coachDao.updataequip(name,price,num);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delEquip(String name) {
        try {
            return coachDao.delEquip(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void showEquip() {
        List<Equip> equips;
        try {
            equips=coachDao.getEquips();
            if (equips!=null){
                for (Equip equip : equips) {
                    System.out.println("装备名称:"+equip.getName());
                    System.out.println("装备价格:"+equip.getPrice());
                    System.out.println("装备数量:"+equip.getNum());
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public boolean addGame(int home, int away, Date date, String loca) {
        try {
            return coachDao.addGame(home,away,date,loca);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
